package com.yanggeng.demo.beginnerguidedemo.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.yanggeng.demo.beginnerguidedemo.R;

/**
 * 签字view
 *
 * @author aiden@tronsis.com
 * @date 2018/5/24 15:46
 */
public class SignView extends View {

    private int mBgColor = Color.TRANSPARENT;

    private int mPaintColor = Color.BLACK;

    private int mPaintWidth = 5;

    private Paint mPaint;

    private Path mPath;

    private Bitmap mCacheBitmap;

    private Canvas mCacheCanvas;

    private float mCurrX, mCurrY;

    public SignView(Context context) {
        super(context);
    }

    public SignView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SignView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setColor(mPaintColor);
        mPaint.setStrokeWidth(mPaintWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);

        mPath = new Path();

    }

    private void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SignView, defStyleAttr, 0);
        int n = typedArray.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.SignView_signViewBackgroundColor:
                    mBgColor = typedArray.getColor(attr, Color.WHITE);
                    break;
                case R.styleable.SignView_paintColor:
                    mPaintColor = typedArray.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.SignView_paintWidth:
                    mPaintWidth = typedArray.getDimensionPixelSize(attr, 5);
                    break;
            }
        }
        typedArray.recycle();
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mCacheBitmap = Bitmap.createBitmap(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec), Bitmap.Config.ARGB_8888);
        mCacheCanvas = new Canvas(mCacheBitmap);
        mCacheCanvas.drawColor(mBgColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mCacheBitmap, 0, 0, null);
        canvas.drawPath(mPath, mPaint);
    }

    public Bitmap getCacheBitmap() {
        return mCacheBitmap;
    }

    public void clear() {
        if (mCacheCanvas != null) {
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            mCacheCanvas.drawPaint(mPaint);
            mCacheCanvas.drawColor(mBgColor);
            mPaint = new Paint();
            mPaint.setColor(mPaintColor);
            mPaint.setStrokeWidth(mPaintWidth);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setAntiAlias(true);
            invalidate();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mCurrX = x;
                mCurrY = y;
                mPath.moveTo(mCurrX, mCurrY);
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.quadTo(mCurrX, mCurrY, x, y);
                mCurrX = x;
                mCurrY = y;
                break;
            case MotionEvent.ACTION_UP:
                mCacheCanvas.drawPath(mPath, mPaint);
                mPath.reset();
                break;
        }

        invalidate();

        return true;
    }
}
